local fushi = fk.CreateSkill{
  name = "pegasus__fushi",
}

local prompts = {}
prompts.discard = "pegasus__fushi-discard"
prompts.replace = "pegasus__fushi-replace"
prompts.log = "pegasus__fushi-log"

Fk:loadTranslationTable{
  ["pegasus__fushi"] = "伏士",
  [":pegasus__fushi"] = "每回合限一次，当一张拼点牌亮出前，你可弃置一张牌并将之替换为【影】。",

  [prompts.discard] = "伏士：可以弃置一张牌，将一张拼点牌替换为【影】",
  [prompts.replace] = "伏士：请选择一张拼点牌，将之替换为【影】",
  [prompts.log] = "%from 发动〖伏士〗将 %to 的拼点牌替换为了 %arg",
}

--- 拿【影】
---@param room Room
---@return integer
local getShade = function (room)
  for _, id in ipairs(room.void) do
    if Fk:getCardById(id).name == "shade" then
      room:setCardMark(Fk:getCardById(id), MarkEnum.DestructIntoDiscard, 1)
      return id
    end
  end

  local card = room:printCard("shade", Card.Spade, 1)
  room:setCardMark(card, MarkEnum.DestructIntoDiscard, 1)
  return card.id
end

fushi:addEffect(fk.PindianCardsDisplayed, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(fushi.name) and not player:isNude() then
      return player:usedSkillTimes(fushi.name, Player.HistoryTurn) == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player, {
      max_num = 1,
      min_num = 1,
      skill_name = fushi.name,
      prompt = prompts.discard,
      include_equip = true,
      skip = true,
    })

    if #card then
      event:setCostData(self, {card = card[1]})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local card = event:getCostData(self).card
    local room = player.room
    room:throwCard(card, fushi.name, player, player)

    local card_data = {}
    table.insert(card_data, { data.from.general, room:getSubcardsByRule(data.fromCard) })
    for p, result in pairs(data.results) do
      if result.toCard then
        table.insert(card_data, { p.general, room:getSubcardsByRule(result.toCard) })
      end
    end

    local op_card = room:askToChooseCard(player, {
      target = player,
      flag = { card_data = card_data },
      skill_name = fushi.name,
      prompt = prompts.replace,
    })

    local new_card = Fk:cloneCard("shade", Card.Spade, 1)
    local shade = getShade(room)
    new_card:addSubcard(shade)

    if room:getSubcardsByRule(data.fromCard)[1] == op_card then
      room:sendLog{
        type = prompts.log,
        from = player.id,
        to = { data.from.id },
        arg = Fk:getCardById(shade):toLogString(),
      }
      data.fromCard = new_card
      data.results[data.from].toCards = room:getSubcardsByRule(new_card)
      
    else
      for p, result in pairs(data.results) do
        if result.toCard and room:getSubcardsByRule(result.toCard)[1] == op_card then
          room:sendLog{
            type = prompts.log,
            from = player.id,
            to = { p.id },
            arg = Fk:getCardById(shade):toLogString(),
          }
          result.toCard = new_card
          result.toCards = room:getSubcardsByRule(new_card)
          break
        end
      end
    end

    room:moveCards({
      ids = { shade },
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = fushi.name,
      moveVisible = true,
      proposer = player,
    }, {
      ids = { op_card },
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonJustMove,
      skillName = fushi.name,
      moveVisible = true,
      proposer = player,
    })
  end
})

return fushi